const cloud = require('wx-server-sdk');
const md5 =require('../mod/md5');

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
});

const db=cloud.database();

exports.main = async (event, context) => {
  const {account,password}=event.data;
  try{
    // const str =md5.hexMD5(password);
    var userinfo;
    switch(event.data.index){
      case 0:
        userinfo=await db.collection("student").where({
          account:account,
          password:password
        }).get();
        break;
      case 1:
        userinfo=await db.collection("admin").where({
          account:account,
          password:password
        }).get();
        break;
      case 2:
        break;
    }
    if(userinfo.data.length==1){
      return {success:true,message:userinfo}
    }else{
      return{success:false,message:userinfo}
    }
    // "账号或密码错误"+account+" "+password
  }catch(error){
      console.error(error);
      return{success:false,message:"登录失败 "+error}
  }
};
